---
description: "Reference for Nitric's Dart library - Subscribe a handler to a topic and receive new events for processing."
---

# Dart - topic.subscribe()

<Note>
  This is reference documentation for the Nitric Dart SDK. To learn about Topics
  and Pub/Sub in Nitric start with the [Async Messaging
  docs](/messaging#topics).
</Note>

Subscribe a handler to a topic and receive new events for processing.

```dart
import 'package:nitric_sdk/nitric.dart';

final updates = Nitric.topic("updates");

updates.subscribe((ctx) async {
  // Log the provided message
  print(ctx.req.json());

  return ctx;
});
```

## Parameters

<Properties>
  <Property name="handler" required type="MessageHandler">
    The middleware (code) to be triggered by the topic.
  </Property>
</Properties>

## Examples

### Subscribe to a topic

```dart
import 'package:nitric_sdk/nitric.dart';

final updates = Nitric.topic("updates");

updates.subscribe((ctx) async {
  // Log the provided message
  print(ctx.req.json());

  return ctx;
});
```

### Notes

- A service may only subscribe to a topic once, if multiple subscribers are required, create them in different service.
- A service may subscribe to OR publish to a topic but not both
